import { reactive, ref } from "vue";
import {
  getRecommendSongList,
  getPrivateContent,
  getRecommendNewSongs,
  getRecommendMV,
  getPlayListCategoryDict,
} from "@/api/index";

import { LyricDataRes } from "@/types/player";

import { getSongLyric } from "@/api/player";

import { hasLogined } from "@/utils/utils";
import { SongList, BroadCastItem, HomeNewSong, MVModel } from "@/types/index";
export function useHomeRecommendSongList() {
  let songListTop = reactive<SongList[]>([]);
  let songListBottom = reactive<SongList[]>([]);

  const updateUseSong = () => {
    const flag = hasLogined();
    getRecommendSongList(8, flag).then((res) => {
      if (res.code == 200) {
        const data = flag ? res.recommend : res.result;
        if (data) {
          data.forEach((item, index) => {
            if (index < 4) {
              songListTop.push(item);
            } else if (index <= 7) {
              songListBottom.push(item);
            }
          });
        }
      }
    });
  };

  return { songListTop, songListBottom, updateUseSong };
}

export function useRecommendNewSongs() {
  let newSongs = ref<HomeNewSong[]>([]);

  const updateUseNewSongs = (limit?: number) => {
    getRecommendNewSongs(limit).then((res) => {
      if (res.code === 200) {
        newSongs.value = res.result;
      }
    });
  };

  return { newSongs, updateUseNewSongs };
}

export function usePrivateContent() {
  let homePrivateContent = ref<BroadCastItem[]>([]);

  getPrivateContent().then((res) => {
    if (res.code === 200) {
      homePrivateContent.value = res.result;
    }
  });

  return { homePrivateContent };
}

export function useHomeMV() {
  let MVList = ref<MVModel[]>([]);

  let UpdateMVList = () => {
    getRecommendMV().then((res) => {
      if (res.code === 200) {
        MVList.value = res.result;
      }
    });
  };

  return { MVList, UpdateMVList };
}

export function useSongLyric() {
  let LyricData = ref<LyricDataRes>();

  const queryLyricData = (id: string | number) => {
    getSongLyric(id).then((res) => {
      LyricData.value = res;
    });
  };

  return { LyricData, queryLyricData };
}
